package cj.web.marry.controllers;

import java.net.URLEncoder;
import java.util.EnumSet;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.paoding.rose.web.annotation.Param;
import net.paoding.rose.web.annotation.Path;
import net.paoding.rose.web.annotation.rest.Get;
import net.paoding.rose.web.annotation.rest.Post;
import net.paoding.rose.web.var.Model;
import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;

import cj.utils.Constants;
import cj.utils.Page;
import cj.utils.weixin.HttpsWenXinUtil;
import cj.utils.weixin.pay.UnifiedOrder;
import cj.web.BaseController;
import cj.web.mall.dao.UserDAO;
import cj.web.marry.constants.EducationEnum;
import cj.web.marry.constants.MemberComTypeEnum;
import cj.web.marry.constants.SalaryEnum;
import cj.web.marry.constants.SexEnum;
import cj.web.marry.constants.WXPayAttachEnum;
import cj.web.marry.constants.XQIsChildrenEnum;
import cj.web.marry.constants.XQIsMarryEnum;
import cj.web.marry.constants.XQIsOpenEnum;
import cj.web.marry.constants.XQOrderStatusEnum;
import cj.web.marry.constants.XQRoomOrCarEnum;
import cj.web.marry.constants.XQYHStatusEnum;
import cj.web.marry.dao.CmpActDAO;
import cj.web.marry.dao.FollowDAO;
import cj.web.marry.dao.PubActDAO;
import cj.web.marry.dao.XQCmpYHDAO;
import cj.web.marry.dao.XQPubYHDAO;
import cj.web.marry.utils.IdcardUtils;

@Path("xqUser")
public class XQUserController extends BaseController{

	@Autowired
	private UserDAO userDao;
	@Autowired
	private CmpActDAO cmpActDAO;
	@Autowired
	private PubActDAO pubActDAO;
	@Autowired
	private XQCmpYHDAO xqCmpYHDAO;
	@Autowired
	private FollowDAO followDao;
	@Autowired
	private XQPubYHDAO xqPubYHDAO;
	
	@Get("cmphomeCode")  //活动页面Code
	public String cmphomeCode(Model model,@Param("code")String code, HttpServletResponse response) throws Exception{
		Map oauth2Reps = HttpsWenXinUtil.retOauth2Infor(code, Constants.weixinkey.AppID, Constants.weixinkey.AppSecret);
		String openId = oauth2Reps.get("openid").toString();
		
//		String access_token = "5_t_0b1DfJd-miqaupR01BIoZkDZBL_FAVKk4TUW3_HTf9MQzfBzWD6tcJNVO0DSdSBmLDWVHckSPoQJOHq8xr9Q";
//		String openId = "ofwuNsyVHbXdpotCwZ_ZJLp7DSrQ";
		setCookie(response, "openId", openId);
		return "r:cmphome";
	}
	
	@Get("cmphome")  //活动页面
	public String cmphome(Model model, HttpServletRequest request) throws Exception{
		
		if( isLogin(request) ){
			
			String phone = getCookieValueByName(request, "phone");
			int buyerId = userDao.getBuyerId(phone);
			int isExist = userDao.isExistMemberDetailCmp(buyerId,MemberComTypeEnum.NUM_1.getValue());
			
			if(isExist==0){
				return "r:../xqLoginCmp/fail";
			}
			
			Map<String, String> config = getConfigSign(request);
			model.add("Config",config);
			
			model.add("Title", "长江婚典商城-您的专属婚品");
			model.add("Desc", "只为您专属打造");
			model.add("LinkURL", oauth_start+request.getScheme() +"://" + request.getServerName() + "/cmpAct/homeCode"+oauth_end);
			
			List<Map> replisUc = userDao.getMemberDetailForUC(buyerId);
			//活动订单
			int cmpActivityOrderCount = userDao.getCmpActivityOrderCount(buyerId);
			//预约数
			int cmpYHCount = userDao.getCmpYHCount(buyerId);
			//关注数
			int followCount = userDao.getFollowCount(buyerId);
			
			for(Map rep:replisUc){
				rep.put("cmpActivityOrderCount", cmpActivityOrderCount);
				rep.put("cmpYHCount", cmpYHCount);
				rep.put("followCount", followCount);
			}
			
			model.add("repUc",replisUc.get(0));
		}else{
			String retPg = URLEncoder.encode("xqUser/cmphome", "utf-8");
			return "r:../xqLoginCmp/login?returnPage=" + retPg;
		}
		
		return "/views/marry/meet/cmpUc/userCenter.vm";
	}
	
	/**
	 * 修改个人公开
	 * @param userId
	 * @param isOpen
	 * @param model
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@Get("cmpChangeIsOpen")
	public String cmpChangeIsOpen(@Param("userId") int userId,@Param("isOpen") int isOpen,
			Model model, HttpServletRequest request) throws Exception{
		
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		userDao.upMemberDetailById(userId, isOpen);
		return "@"+jo.toString();
	}
	
	@Get("cmpActivity")  //cmp活动页面
	public String cmpActivity(Model model, HttpServletRequest request) throws Exception{
		if( !isLogin(request) ){
			String retPg = URLEncoder.encode("xqUser/home", "utf-8");
			return "r:../user/login?returnPage=" + retPg;
		}
		return "/views/marry/meet/cmpUc/cmpActivitylis.vm";
	}
	
	@Get("cmpActivitylis")  //cmp活动列表方法
	public String cmpActivitylis(@Param("start") int start,@Param("length") int length,
			Model model, HttpServletRequest request) throws Exception{
		
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		String phone = getCookieValueByName(request, "phone");
		int buyerId = userDao.getBuyerId(phone);
		
		List<Map> gamels = cmpActDAO.queryCmpActivityOrder(buyerId,start*length,length);
		for(Map rep:gamels){
			rep.put("statusName", 
					XQOrderStatusEnum.getName(Integer.parseInt(rep.get("status").toString())));
		}
		
		int totalCount = cmpActDAO.queryCmpActivityOrderCount(buyerId);
		//分页计算
		Page p= new Page<Map>(0, length, gamels, totalCount);
		jo.put("list", gamels);
		jo.put("repsize", p.getTotalPages());
		jo.put("totalCount", totalCount);
		
		return "@"+jo.toString();
	}
	
	/**
	 * cmp活动详情
	 * @param model
	 * @param gameId	活动ID
	 * @return
	 * @throws Exception 
	 */
	@Get("cmpActivitydetail")
	public String cmpActivitydetail(Model model,@Param("id")int id,
			@Param("cmpActivityId")int cmpActivityId, HttpServletRequest request) throws Exception{
			
		List<Map> cmpActlis = cmpActDAO.queryById(cmpActivityId);
		
		if(cmpActlis!=null && cmpActlis.size()>0){
			Map<String, String> config = getConfigSign(request);
			model.add("Config",config);
			String gameName = cmpActlis.get(0).get("name").toString();
			model.add("Title", gameName);
			model.add("Desc", gameName);
			model.add("LinkURL", oauth_start+request.getScheme() +"://" + request.getServerName() + "/pubAct/detailCode"+oauth_end);
			
			List<Map> cmpActOrderlis = cmpActDAO.queryCmpActivityOrderById(id);
			model.add("status", cmpActOrderlis.get(0).get("status").toString());
			//活动详情
			model.add("obj", cmpActlis.get(0));
			model.add("XQOrderStatusEnum",EnumSet.allOf(XQOrderStatusEnum.class));
		}
		
		return "/views/marry/meet/cmpUc/cmpActivityDetail.vm";
	}
	
	/**
	 * 个人主页
	 * @param model
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@Get("userHome")
	public String userHome(Model model, HttpServletRequest request) throws Exception{
		String phone = getCookieValueByName(request, "phone");
		int buyerId = userDao.getBuyerId(phone);
		
		List<Map> repUcLis = userDao.getMemberDetailByUserId(buyerId);
		model.add("userObj", repUcLis.get(0));
		//MemuEnum
        model.add("ComTypeEnum",EnumSet.allOf(MemberComTypeEnum.class));
        model.add("SexEnum",EnumSet.allOf(SexEnum.class));
        model.add("IsMarryEnum",EnumSet.allOf(XQIsMarryEnum.class));
        model.add("IsOpenEnum",EnumSet.allOf(XQIsOpenEnum.class));
        model.add("IsChildrenEnum",EnumSet.allOf(XQIsChildrenEnum.class));
        model.add("XQRoomOrCarEnum",EnumSet.allOf(XQRoomOrCarEnum.class));
        model.add("EducationEnum",EnumSet.allOf(EducationEnum.class));
        model.add("SalaryEnum",EnumSet.allOf(SalaryEnum.class));
		return "/views/marry/meet/userHome.vm";
	}
	
	/**
	 * 个人主页
	 * @param model
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@Get("userDetail")
	public String userDetail(Model model,@Param("userId") int userId) throws Exception{
		
		List<Map> repUcLis = userDao.getMemberDetailByUserId(userId);
		model.add("userObj", repUcLis.get(0));
		//MemuEnum
        model.add("ComTypeEnum",EnumSet.allOf(MemberComTypeEnum.class));
        model.add("SexEnum",EnumSet.allOf(SexEnum.class));
        model.add("IsMarryEnum",EnumSet.allOf(XQIsMarryEnum.class));
        model.add("IsOpenEnum",EnumSet.allOf(XQIsOpenEnum.class));
        model.add("IsChildrenEnum",EnumSet.allOf(XQIsChildrenEnum.class));
        model.add("XQRoomOrCarEnum",EnumSet.allOf(XQRoomOrCarEnum.class));
        model.add("EducationEnum",EnumSet.allOf(EducationEnum.class));
        model.add("SalaryEnum",EnumSet.allOf(SalaryEnum.class));
		return "/views/marry/meet/memberDetail.vm";
	}
	
	/**
	 * 关于我们
	 * @return
	 * @throws Exception
	 */
	@Get("about")
	public String about() throws Exception{
		return "/views/marry/meet/about.vm";
	}
	
	/**
	 * 用户协议
	 * @return
	 * @throws Exception
	 */
	@Get("userAgreement")
	public String userAgreement() throws Exception{
		return "/views/marry/meet/userAgreement.vm";
	}
	
	/**
	 * 修改个人信息
	 * @param model
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@Get("userEdit")
	public String userEdit(Model model, HttpServletRequest request,@Param("returnPage") String returnPage) throws Exception{
		
		String phone = getCookieValueByName(request, "phone");
		int buyerId = userDao.getBuyerId(phone);
			
		List<Map> objActlis = userDao.getMemberDetailByUserId(buyerId);
		
		if(objActlis!=null && objActlis.size()>0){
			//活动详情
			model.add("obj", objActlis.get(0));
		}
		model.add("ComTypeEnum",EnumSet.allOf(MemberComTypeEnum.class));
        model.add("SexEnum",EnumSet.allOf(SexEnum.class));
        model.add("IsMarryEnum",EnumSet.allOf(XQIsMarryEnum.class));
        model.add("IsOpenEnum",EnumSet.allOf(XQIsOpenEnum.class));
        model.add("IsChildrenEnum",EnumSet.allOf(XQIsChildrenEnum.class));
        model.add("XQRoomOrCarEnum",EnumSet.allOf(XQRoomOrCarEnum.class));
        model.add("EducationEnum",EnumSet.allOf(EducationEnum.class));
        model.add("SalaryEnum",EnumSet.allOf(SalaryEnum.class));
        model.add("returnPage",returnPage);
		
		return "/views/marry/meet/userEdit.vm";
	}
	
	/**
	 * 完善个人信息
	 * @param name
	 * @param files
	 * @param identityCard
	 * @param nickname
	 * @param sex
	 * @param isMarry
	 * @param weight
	 * @param height
	 * @param isOpen
	 * @param education
	 * @param school
	 * @param profession
	 * @param money
	 * @param roomOrCar
	 * @param isChildren
	 * @param infor
	 * @param selection
	 * @param province
	 * @param city
	 * @param area
	 * @param degree
	 * @param request
	 * @return
	 */
	@Post("perfect")
	public synchronized String perfect(@Param("photo") String photo,@Param("companyName")String companyName,
			@Param("identityCard")String identityCard,@Param("nickname")String nickname,
			@Param("sex")int sex,@Param("isMarry")int isMarry,@Param("weight")String weight,@Param("height")String height,
			@Param("isOpen")int isOpen,@Param("education")String education,@Param("school")String school,
			@Param("profession")String profession,@Param("money")String money,@Param("roomOrCar")String roomOrCar,
			@Param("isChildren")int isChildren,@Param("infor")String infor,@Param("selection")String selection,
			@Param("province")String province,@Param("city")String city,@Param("area")String area,@Param("degree")int degree,
			HttpServletRequest request){
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		try {
			String phone = getCookieValueByName(request, "phone");
			int buyerId = userDao.getBuyerId(phone);
			
			area=province.split(" ")[2];
			city=province.split(" ")[1];
			province=province.split(" ")[0];
			
			List<Map> repUc = userDao.getMemberDetailForUC(buyerId);
			
			//人员类型
			int type = Integer.parseInt(repUc.get(0).get("type").toString());
			
			if(MemberComTypeEnum.NUM_0.getValue()==type){
				//个人
				userDao.update(photo, nickname, sex, isMarry, weight, 
	            		height, isOpen, education, school, profession, 
	            		money, roomOrCar, isChildren, infor, selection, 
	            		province, city, area, phone, companyName, buyerId);
				
			}else{
				//单位
				userDao.update(photo, nickname, sex, isMarry, weight, 
	            		height, isOpen, education, school, profession, 
	            		money, roomOrCar, isChildren, infor, selection, 
	            		province, city, area, phone, buyerId);
				
			}
			
		} catch (Exception e) {
			// TODO: handle exception
			jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
		}
		
		return "@" + jo.toString();
	}
	
	//-----------------------
	@Get("pubhomeCode")  //活动页面Code
	public String pubhomeCode(Model model,@Param("code")String code, HttpServletResponse response) throws Exception{
		Map oauth2Reps = HttpsWenXinUtil.retOauth2Infor(code, Constants.weixinkey.AppID, Constants.weixinkey.AppSecret);
		String openId = oauth2Reps.get("openid").toString();
		
//		String access_token = "5_t_0b1DfJd-miqaupR01BIoZkDZBL_FAVKk4TUW3_HTf9MQzfBzWD6tcJNVO0DSdSBmLDWVHckSPoQJOHq8xr9Q";
//		String openId = "ofwuNsyVHbXdpotCwZ_ZJLp7DSrQ";
		setCookie(response, "openId", openId);
		return "r:pubhome";
	}
	
	@Get("pubhome")  //活动页面
	public String pubhome(Model model, HttpServletRequest request) throws Exception{
		
		if( isLogin(request) ){
			
			String phone = getCookieValueByName(request, "phone");
			int buyerId = userDao.getBuyerId(phone);
			int isExist = userDao.isExistMemberDetail(buyerId);
			
			if(isExist==0){
				String retPg = URLEncoder.encode("xqUser/pubMember", "utf-8");
				return "r:../xqLoginPub/perfectUser?returnPage="+retPg;
			}
			
			Map<String, String> config = getConfigSign(request);
			model.add("Config",config);
			
			model.add("Title", "长江婚典商城-您的专属婚品");
			model.add("Desc", "只为您专属打造");
			model.add("LinkURL", oauth_start+request.getScheme() +"://" + request.getServerName() + "/pubAct/homeCode"+oauth_end);
			
			List<Map> replisUc = userDao.getMemberDetailForUC(buyerId);
			//活动订单
			int pubActivityOrderCount = userDao.getPubActivityOrderCount(buyerId);
			//预约数
			int pubYHCount = userDao.getPubYHCount(buyerId);
			//关注数
			int followCount = userDao.getFollowCount(buyerId);
			
			for(Map rep:replisUc){
				rep.put("pubActivityOrderCount", pubActivityOrderCount);
				rep.put("pubYHCount", pubYHCount);
				rep.put("followCount", followCount);
			}
			
			model.add("repUc",replisUc.get(0));
		}else{
			String retPg = URLEncoder.encode("xqUser/pubhome", "utf-8");
			return "r:../user/login?returnPage=" + retPg;
		}
		
		return "/views/marry/meet/pubUc/userCenter.vm";
	}
	
	@Get("pubActivity")  //cmp活动页面
	public String pubActivity(Model model, HttpServletRequest request) throws Exception{
		if( !isLogin(request) ){
			String retPg = URLEncoder.encode("xqUser/pubhome", "utf-8");
			return "r:../user/login?returnPage=" + retPg;
		}
		return "/views/marry/meet/pubUc/pubActivitylis.vm";
	}
	
	@Get("pubActivitylis")  //cmp活动列表方法
	public String pubActivitylis(@Param("start") int start,@Param("length") int length,
			Model model, HttpServletRequest request) throws Exception{
		
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		String phone = getCookieValueByName(request, "phone");
		int buyerId = userDao.getBuyerId(phone);
		
		List<Map> gamels = pubActDAO.queryPubActivityOrder(buyerId,start*length,length);
		for(Map rep:gamels){
			rep.put("statusName", 
					XQOrderStatusEnum.getName(Integer.parseInt(rep.get("status").toString())));
		}
		
		int totalCount = pubActDAO.queryPubActivityOrderCount(buyerId);
		//分页计算
		Page p= new Page<Map>(0, length, gamels, totalCount);
		jo.put("list", gamels);
		jo.put("repsize", p.getTotalPages());
		jo.put("totalCount", totalCount);
		
		return "@"+jo.toString();
	}
	
	/**
	 * cmp活动详情
	 * @param model
	 * @param gameId	活动ID
	 * @return
	 * @throws Exception 
	 */
	@Get("pubActivitydetail")
	public String pubActivitydetail(Model model,@Param("id")int id,
			@Param("pubActivityId")int pubActivityId, HttpServletRequest request) throws Exception{
			
		List<Map> pubActlis = pubActDAO.queryById(pubActivityId);
		
		if(pubActlis!=null && pubActlis.size()>0){
			Map<String, String> config = getConfigSign(request);
			model.add("Config",config);
			String gameName = pubActlis.get(0).get("name").toString();
			model.add("Title", gameName);
			model.add("Desc", gameName);
			model.add("LinkURL", oauth_start+request.getScheme() +"://" + request.getServerName() + "/pubAct/detailCode"+oauth_end);
			
			List<Map> pubActOrderlis = pubActDAO.queryPubActivityOrderById(id);
			model.add("status", pubActOrderlis.get(0).get("status").toString());
			model.add("orderId", id);
			//活动详情
			model.add("obj", pubActlis.get(0));
			model.add("XQOrderStatusEnum",EnumSet.allOf(XQOrderStatusEnum.class));
		}
		
		return "/views/marry/meet/pubUc/pubActivityDetail.vm";
	}
	
	/**
	 * 微信支付
	 * @param orderId
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@Post("pubActWxpay")
	public String pubActWxpay(@Param("orderId")int orderId, HttpServletRequest request) throws Exception{
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		String price = "",
				orderNo = "";
		List<Map> orders = pubActDAO.queryPubActivityOrderById(orderId);
		if( orders != null && orders.size() > 0 ){ //验证订单是否存在
			Map order = orders.get(0);
			int status = Integer.parseInt(order.get("status").toString());
			if( status == XQOrderStatusEnum.finish.getValue() ){
				jo.put("returnFlag", Constants.returnObj.returnFlag_flase); //是否已付款
				jo.put("returnCode", Constants.returnObj.returnCode.code_2);
				return "@" + jo.toString();
			}
			price = order.get("price").toString();
			orderNo = order.get("orderNo").toString();
		}
		else{
			jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
			jo.put("returnCode", Constants.returnObj.returnCode.code_1);
			return "@" + jo.toString();
		}
		
		//调用微信接口
		String tmpPrice = String.valueOf(Float.parseFloat(price) * 100);
		String total_fee = tmpPrice.substring(0, tmpPrice.indexOf("."));
		String spbill_create_ip = request.getRemoteAddr();
		String notify_url = request.getScheme() +"://" + request.getServerName() + "/order/result";
        String openId = getCookieValueByName(request, "openId");
        if(openId != null && openId.length() > 0){
        	jo.put("hasOpenId", true);
        	UnifiedOrder unifiedOrder = new UnifiedOrder();
    		Map prepay = unifiedOrder.getPrepayId("确认订单-我的报名", orderNo, total_fee, spbill_create_ip, notify_url, openId ,WXPayAttachEnum.XQ.getValue());
    		jo.put("rtn", prepay);
        }
        else{
        	jo.put("hasOpenId", false);
        }
        
        return "@" + jo.toString();
	}
	
	
	//----------------------------------
	@Get("cmpAppoint")
	public String cmpAppoint() throws Exception{
		return "/views/marry/meet/cmpUc/userDating.vm";
	}
	
	@Get("cmpAppointlis")
	public String cmpAppointlis(Model model,@Param("start") int start,
			@Param("length") int length, HttpServletRequest request) throws Exception{
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		String phone = getCookieValueByName(request, "phone");
		int buyerId = userDao.getBuyerId(phone);
		
		List<Map> gamels = xqCmpYHDAO.queryCmpYH(buyerId,start*length,length);
		
		for(Map rep:gamels){
			rep.put("age", IdcardUtils.getAgeByIdCard(rep.get("identityCard").toString()));
			rep.put("appointStatusName", XQYHStatusEnum.getName(Integer.parseInt(rep.get("appointStatus").toString())));
		}
		
		int totalCount = xqCmpYHDAO.queryCmpYHSize(buyerId);
		//分页计算
		Page p= new Page<Map>(0, length, gamels, totalCount);
		jo.put("list", gamels);
		jo.put("repsize", p.getTotalPages());
		jo.put("totalCount", totalCount);
		return "@"+jo.toString();
	}
	
	@Get("cmpFollow")
	public String cmpFollow() throws Exception{
		return "/views/marry/meet/cmpUc/userFollow.vm";
	}
	
	@Get("cmpFollowlis")
	public String cmpFollowlis(Model model,@Param("start") int start,
			@Param("length") int length, HttpServletRequest request) throws Exception{
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		String phone = getCookieValueByName(request, "phone");
		int buyerId = userDao.getBuyerId(phone);
		
		List<Map> gamels = followDao.queryByUserId(buyerId,start*length,length);
		
		for(Map rep:gamels){
			rep.put("age", IdcardUtils.getAgeByIdCard(rep.get("identityCard").toString()));
		}
		
		int totalCount = followDao.querySizeByUserId(buyerId);
		//分页计算
		Page p= new Page<Map>(0, length, gamels, totalCount);
		jo.put("list", gamels);
		jo.put("repsize", p.getTotalPages());
		jo.put("totalCount", totalCount);
		return "@"+jo.toString();
	}
	
	//--------------------------------
	@Get("pubMemberCode")  //会员推荐
	public String pubMemberCode(Model model,@Param("code")String code, HttpServletResponse response) throws Exception{
		Map oauth2Reps = HttpsWenXinUtil.retOauth2Infor(code, Constants.weixinkey.AppID, Constants.weixinkey.AppSecret);
		String openId = oauth2Reps.get("openid").toString();
		
//		String access_token = "5_t_0b1DfJd-miqaupR01BIoZkDZBL_FAVKk4TUW3_HTf9MQzfBzWD6tcJNVO0DSdSBmLDWVHckSPoQJOHq8xr9Q";
//		String openId = "ofwuNsyVHbXdpotCwZ_ZJLp7DSrQ";
		setCookie(response, "openId", openId);
		return "r:pubMember";
	}
	
	@Get("pubMember")  //会员推荐
	public String pubMember(Model model, HttpServletRequest request) throws Exception{
		
		if( isLogin(request) ){
			
			String phone = getCookieValueByName(request, "phone");
			int buyerId = userDao.getBuyerId(phone);
			int isExist = userDao.isExistMemberDetail(buyerId);
			
			if(isExist==0){
				String retPg = URLEncoder.encode("xqUser/pubMember", "utf-8");
				return "r:../xqLoginPub/perfectUser?returnPage="+retPg;
			}
			
			Map<String, String> config = getConfigSign(request);
			model.add("Config",config);
			
			model.add("Title", "长江婚典商城-您的专属婚品");
			model.add("Desc", "只为您专属打造");
			model.add("LinkURL", oauth_start+request.getScheme() +"://" + request.getServerName() + "/pubAct/homeCode"+oauth_end);
			
			List<Map> replisUc = userDao.getMemberDetailForUC(buyerId);
			//活动订单
			int pubActivityOrderCount = userDao.getPubActivityOrderCount(buyerId);
			//预约数
			int pubYHCount = userDao.getPubYHCount(buyerId);
			//关注数
			int followCount = userDao.getFollowCount(buyerId);
			
			for(Map rep:replisUc){
				rep.put("pubActivityOrderCount", pubActivityOrderCount);
				rep.put("pubYHCount", pubYHCount);
				rep.put("followCount", followCount);
			}
			
			model.add("repUc",replisUc.get(0));
		}else{
			String retPg = URLEncoder.encode("xqUser/pubMember", "utf-8");
			return "r:../xqLoginPub/login?returnPage=" + retPg;
		}
		
		return "/views/marry/meet/pubMember/userCenter.vm";
	}
	
	@Get("pubAppoint")
	public String pubAppoint() throws Exception{
		return "/views/marry/meet/pubMember/userDating.vm";
	}
	
	@Get("pubAppointlis")
	public String pubAppointlis(Model model,@Param("start") int start,
			@Param("length") int length, HttpServletRequest request) throws Exception{
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		String phone = getCookieValueByName(request, "phone");
		int buyerId = userDao.getBuyerId(phone);
		
		List<Map> gamels = xqPubYHDAO.queryPubYH(buyerId,start*length,length);
		
		for(Map rep:gamels){
			rep.put("age", IdcardUtils.getAgeByIdCard(rep.get("identityCard").toString()));
			rep.put("appointStatusName", XQYHStatusEnum.getName(Integer.parseInt(rep.get("appointStatus").toString())));
		}
		
		int totalCount = xqPubYHDAO.queryPubYHSize(buyerId);
		//分页计算
		Page p= new Page<Map>(0, length, gamels, totalCount);
		jo.put("list", gamels);
		jo.put("repsize", p.getTotalPages());
		jo.put("totalCount", totalCount);
		return "@"+jo.toString();
	}
	
	@Get("pubFollow")
	public String pubFollow() throws Exception{
		return "/views/marry/meet/pubMember/userFollow.vm";
	}
	
	@Get("pubFollowlis")
	public String pubFollowlis(Model model,@Param("start") int start,
			@Param("length") int length, HttpServletRequest request) throws Exception{
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		String phone = getCookieValueByName(request, "phone");
		int buyerId = userDao.getBuyerId(phone);
		
		List<Map> gamels = followDao.queryByUserId(buyerId,start*length,length);
		
		for(Map rep:gamels){
			rep.put("age", IdcardUtils.getAgeByIdCard(rep.get("identityCard").toString()));
		}
		
		int totalCount = followDao.querySizeByUserId(buyerId);
		//分页计算
		Page p= new Page<Map>(0, length, gamels, totalCount);
		jo.put("list", gamels);
		jo.put("repsize", p.getTotalPages());
		jo.put("totalCount", totalCount);
		return "@"+jo.toString();
	}
	
	/**
	 * 个人主页
	 * @param model
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@Get("userPubDetail")
	public String userPubDetail(Model model,@Param("userId") int userId) throws Exception{
		
		List<Map> repUcLis = userDao.getMemberDetailByUserId(userId);
		model.add("userObj", repUcLis.get(0));
		//MemuEnum
        model.add("ComTypeEnum",EnumSet.allOf(MemberComTypeEnum.class));
        model.add("SexEnum",EnumSet.allOf(SexEnum.class));
        model.add("IsMarryEnum",EnumSet.allOf(XQIsMarryEnum.class));
        model.add("IsOpenEnum",EnumSet.allOf(XQIsOpenEnum.class));
        model.add("IsChildrenEnum",EnumSet.allOf(XQIsChildrenEnum.class));
        model.add("XQRoomOrCarEnum",EnumSet.allOf(XQRoomOrCarEnum.class));
        model.add("EducationEnum",EnumSet.allOf(EducationEnum.class));
        model.add("SalaryEnum",EnumSet.allOf(SalaryEnum.class));
		return "/views/marry/meet/pubMember/memberDetail.vm";
	}
}